package com.tjise.vod.controller;

import com.aliyuncs.DefaultAcsClient;


import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.tjise.commonutils.R;
import com.tjise.servicebase.exceptionhandler.TjiseException;
import com.tjise.vod.service.VodService;
import com.tjise.vod.util.ConstantVodUtils;
import com.tjise.vod.util.InitVdoClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @program: tjise_shixun
 * @description: video
 * @author: 郑朝文
 * @create: 2021-06-25 18:11
 **/
@Api(description = "视频管理")
@RestController

@RequestMapping("/eduvod/video")
@Slf4j
public class VodController {

    @Autowired
    private VodService vodService;

    /**
     * 上传到阿里云的视频
     */

    @ApiOperation("郑朝文写的 上传视频的功能")
    @PostMapping("uploadAliYunVideo")
    public R uploadAliYunVideo(MultipartFile file) {
        // 调用service中的上传业务方法
        String videoId = vodService.uploadVideo(file);
        return R.ok().data("videoId", videoId);

    }

    /**
     * 根据视频id删除阿里云的视频
     */
    @ApiOperation("郑朝文写的删除视频")
    @DeleteMapping("removeAlyVideo/{id}")
    public R removeAlyVideo(@PathVariable("id") String id) {
        try {


            log.info("-0----------------id ----------"+id);
            // 初始化对象
            DefaultAcsClient client = InitVdoClient.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            // 创建删除视频的Request
            DeleteVideoRequest request = new DeleteVideoRequest();
            // 设置视频id
            request.setVideoIds(id);
            // 调用初始化对象删除
            client.getAcsResponse(request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            throw new TjiseException(20001, "删除视频失败");

        }


    }

    /**
     * 删除阿里云多个视频
     */
    @ApiOperation("郑朝文写的删除多个视频")
    @DeleteMapping("delete-batch")
    public R deleteBatch(@RequestParam("videoIdList") List<String> videoIdList) {
        vodService.removeAlyVideo(videoIdList);
        return R.ok();
    }
    /**
     * 根据视频id 获取视频的播放凭证
     */
    @ApiOperation("郑朝文写的获取视频的凭证")
    @GetMapping("/getPlayAuthor/{id}")
    public  R getPlayAuthor(@PathVariable("id")String id){
        // 初始化
        DefaultAcsClient defaultAcsClient = null;
        try {
            defaultAcsClient = InitVdoClient.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            // 创建request对象
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();

            // 通过视频id获取
            request.setVideoId(id);
            // 调用getAcsResponse() 方法返回
            GetVideoPlayAuthResponse response = defaultAcsClient.getAcsResponse(request);
            // 获取凭证
            String playAuth = response.getPlayAuth();
            log.info("播放凭证为："+playAuth);
            return R.ok().data("playAuth",playAuth);
        } catch (ClientException e) {

            throw new TjiseException(20001,"获取凭证失败");
        }
    }


}


